<?php
header('Content-Type: application/xml');

if(!empty($_GET["root"])) {
	$tv = new cls_treeview();
	$tv->root = $_GET["root"];
	echo $tv->showTree();
} else { 
	exit; 
}

class cls_treeview {
	var $root = "";
	var $db = "";
	
	/* predifined functions*/
	function __construct() {}
	function __set($set, $value) { eval("\$this->".$set."='".$value."';"); }
	function __get($get) { eval("return \$this->".$get.";"); }
	function __destruct() {}
	/*end predifined functions */
	
	/* common functions */
	function r_print($array) { echo "<pre>\n"; print_r($array); echo "</pre>\n"; }
	/* end common functions */

	function showTree() {
		$_browse = new cls_browse();
		$array = $_browse->f_getDir($this->root);
		$cnt = 0;

		$tmp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\r";
		$tmp .= "<root>\n\r";
		foreach($array as $key=>$value) {
			$cnt++;
			$tmp .= "\t<dir>\n\r";
			$tmp .= "\t\t<name>$value</name>\n\r";
			$tmp .= "\t\t<path>$this->root$value/</path>\n\r";
			$tmp .= "\t\t<children>".$_browse->f_cntDir($this->root.$value."/")."</children>\n\r";
			$tmp .= "\t</dir>\n\r";
		}
		$tmp .= "</root>\n\r";
		return $tmp;
	}
	
}

class cls_browse {

	/* predifined functions*/
	function __construct() {}
	function __set($set, $value) { eval("\$this->".$set."='".$value."';"); }
	function __get($get) { eval("return \$this->".$get.";"); }
	function __destruct() {}
	/*end predifined functions */
	
	function f_cntDir($dir) {
		$cnt=0;
		if ($handle = opendir($dir)) {	
			while (false !== ($file = readdir($handle))) { 
	    			if(is_dir($dir.$file)){				
						if($file != "." && $file !=".."){
							$cnt++;
						}
					}
			}
			closedir($handle); 
		}
		return $cnt;
	}

	function f_getDir($dir) {	
		if ($handle = opendir($dir)) {	
			while (false !== ($file = readdir($handle))) { 
	    			if(is_dir($dir.$file)){				
						if($file != "." && $file !=".."){
							$tmp[]=$file;
						}
					}
			}
	    closedir($handle); 
		}
		return $tmp;
	}
}
?>